#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct staff
{
    int number;
    char name[50];
    char position[50];
    char department[50];
    int YearOfEntry;
}s;
typedef struct salary
{
    int wage;
    char level;
}$;
typedef struct node
{
    s * pp;
    $ * mony;
    struct node * next;
}NODE;
NODE * creatnode(s * t)
{
    NODE * head=malloc(sizeof(NODE));
    head->pp=NULL;
    head->next=NULL;
    return head;
}
NODE * addnode(NODE * head,s * t,$ * w)
{
    NODE * p= head;
    NODE * new=malloc(sizeof(NODE));
    new->pp=t;
    new->mony=w;
    new->next=NULL;//新结点也得写好空
    while (p->next!=NULL)
    {
        p=p->next;
    }

    p->next=new;
    // printf("***%cyyyy%d****\n",p->mony->level,p->mony->wage);
    return head;
}
NODE * initialize()
{
    NODE *phead=creatnode(NULL);
    FILE * fp=fopen("staff.txt","r");
    FILE * pf=fopen("wage.txt","r");
    while (1)
    {
        // printf("------\n");
        s * t=malloc(sizeof(s));
        $ * w=malloc(sizeof($));
        //用fscanf数据要用空格隔开
        fscanf(fp,"%d %s %s %s %d",&t->number,t->name,t->position,t->department,&t->YearOfEntry);
        if(fscanf(pf,"%s %c %d",t->name,&w->level,&w->wage)!=EOF)//fscanf到文件末尾时返回EOF(-1);
        {
            // printf("***%ctttt%d****\n",w->level,w->wage);
        }
        else
        w=NULL;
        if(feof(fp))//读到文件末尾,放到增加结点前，以防创建空结点,因为文件中空的一行也算内容。
        {
            break;
        }
        addnode(phead,t,w);
       
                    
    }
    fclose(fp);
    fclose(pf);
    NODE * p=phead;
    
    return phead;
}
int LoginInterface(NODE * head)//登录界面
{ 
    while (1)
    {
        FILE * fp=fopen("interface.txt","w+");//可读可写
        fputs("*************\n",fp);
        fputs("1.登录 2.注册\n",fp);
        fputs("*************\n",fp);
        rewind(fp);
        char str[50];
        while (fgets(str,50,fp)!=NULL)
        {
            printf("%s",str);  
        }
        fclose(fp);
        int a;
        scanf("%d",&a);
        if (a==1)
        {
            char ID[20];
            printf("请输入用户名:");
            scanf("%s",ID);
            if (strcmp("root",ID)==0)
            {   
                int pwd;
                printf("管理员密码:");
                scanf("%d",&pwd);
                if (pwd=12345)
                {
                    return 0;
                }  
            }
            NODE * p=head;
            while (p->next!=NULL)
            {
                p=p->next;
                char s[20];
                sprintf(s,"%d",p->pp->number);//将p->pp->number用字符串的形式装起来。
                if (strcmp(s,ID)==0)
                {
                    FILE * fp=fopen("passwd.txt","r");
                    int f=0,n,pwd,k;
                    while (fscanf(fp,"%d %d",&n,&pwd)!=EOF)//将文件的内容输出到变量，到文件末尾自动结束
                    {
                        char N[20];
                        sprintf(N,"%d",n);
                        if (strcmp(N,ID)==0)
                        {
                            printf("请输入员工密码:");
                            scanf("%d",&k);
                            if (k==pwd)
                            {
                                printf("登录成功\n");
                                return n;
                            }
                            f=1;
                        } 
                    }
                    if (f==0)
                    {
                        printf("未注册账号");
                    }                  
                }                   
            }
            printf("用户名或者密码错误\n");
        }
        if (a==2)
        {
            int b,f=0;
            FILE * fp=fopen("passwd.txt","a+");
            printf("请输入员工编号:");
            scanf("%d",&b);
            NODE * t=head;
            while (t->next!=NULL)
            {
                t=t->next;
                if (t->pp->number==b)//匹配员工编号
                {
                    int pwd,pwd1;
                    printf("设置你的密码为:");
                    scanf("%d",&pwd);
                    fprintf(fp,"%d %d\n",b,pwd);
                    printf("员工%s注册账号成功\n",t->pp->name);
                    f=1;
                    break;
                    
                }
            }
            if (f==0)//未匹配成功
            {
                printf("查无此人\n");
            }          
            fclose(fp);
        }      
    }         
}
int InitialInterface(int n)//操作指南界面
{
   
    if (n==0)
    {
        FILE *fp=fopen("Initial.txt","w+");
        fputs("***********************************管理员操作中心**************************************\n",fp);
        fputs("1.添加新员工 2.删除员工 3.查找员工 4.修改员工信息 5.显示所有员工信息 6.工资、等级 7.退出系统\n",fp);
        fputs("**************************************************************************************\n",fp);
        rewind(fp);
        while (feof(fp)!=1)
        {
            char str[100]={0};
            fgets(str,100,fp);
            printf("%s",str);
        }
        fclose(fp); 
    }
    else if (n==2)
    {
        FILE * fp=fopen("staffinterface.txt","w+");
        fputs("*************************员工服务中心************************\n",fp);
        fputs("1.查看本月工资,目前等级 2.修改密码 3.员工圈 4.预支工资 5.切换用户\n",fp);
        fputs("*************************************************************\n",fp);
        rewind(fp);
        while (feof(fp)!=1)
        {
            char str[100]={0};
            fgets(str,100,fp);
            printf("%s",str);
        }
        fclose(fp);
    }  
}
void addmember()//添加员工
{   
    NODE * head=initialize();
    FILE *fp=fopen("staff.txt","aw+"); 
    if (fp == NULL) {  
        printf("Unable to open file\n");  
        return;  
    }
    printf("输入1添加,输入2返回上一项\n");
    while (1)
    {
        int a;
        scanf("%d",&a);
        if(a==1)
        {
            s * t=malloc(sizeof(s));
            $ * w=NULL;//添加员工时工资和等级先给空。
            printf("请依次输入员工编号 员工姓名 职位 部门 入职年份\n");
            scanf("%d %s %s %s %d",&t->number,t->name,t->position,t->department,&t->YearOfEntry);
            addnode(head,t,w);
            fprintf(fp,"%d\t%s\t%s\t%s\t%d\n",t->number,t->name,t->position,t->department,t->YearOfEntry);
            printf("员工信息已录入完毕,输入1继续添加,输入2返回上一项\n");     
        }
        else if(a==2)
        break;
    }
    fclose(fp);
}
void Delstaff()//删除员工
{
    NODE * head=initialize();
    int n;  
    while (1)
    {
        printf("输入1移除,输入2返回上一项\n");
        int a;
        scanf("%d",&a);
        if(a==1)
        {
            NODE *p=head;//用于查找编号
            NODE *t=head;//用于更新员工名单
            NODE *q=head;//用于更新工资文件
            int f=0;
            printf("请输入你要移除的人员编号\n");
            scanf("%d",&n);
            
            while (p->next!=NULL)
            {              
                if(p->next->pp->number==n)
                {
                    NODE * del=p->next;
                    p->next=p->next->next;
                    free(del);
                    printf("已成功移除编号为%d的员工\n",n);
                    f=1;
                }
                if (p->next==NULL&&f==1)
                {
                    break;
                }
                p=p->next; 
                
            }
            if(p->next==NULL&&f==0)
            {
                printf("查无此人\n");
            }
            FILE *fp=fopen("staff.txt","w+");//w+是以清空文件的形式打开文件读写
            while (t->next!=NULL)
            {
                t=t->next;
                fprintf(fp,"%d\t%s\t%s\t%s\t%d\n",t->pp->number,t->pp->name,t->pp->position,t->pp->department,t->pp->YearOfEntry);
            }
            fclose(fp);
            FILE * pf=fopen("wage.txt","w+");
         
            while (q->next!=NULL)
            {
                q=q->next;
                if(q->mony!=NULL)
                fprintf(pf,"%s %c %d\n",q->pp->name,q->mony->level,q->mony->wage);
            }
            fclose(pf);
        }
        else if(a==2)
        break;
    }
}
void findmember()//查找员工
{
    NODE * head=initialize();
    int n;
    printf("输入1通过编号查找,输入2通过姓名查找\n");
    scanf("%d",&n);
    if(n==1)
    {
        NODE *p=head;//遍历匹配编号
        
        int f=0;
        int a;
        printf("请输入编号\n");
        scanf("%d",&a);
        while (p->next!=NULL)
        {
            p=p->next;
            if(p->pp->number==a)
            {
                
                printf("编号%d\t姓名:%s\t职位:%s\t部门:%s入职时间:%d\n",p->pp->number,p->pp->name,p->pp->position,p->pp->department,p->pp->YearOfEntry);
                f=1;
            }
            if (p->next==NULL&&f==1)
            {
                break;
            }     
        }
        if(p->next==NULL&&f==0)
        {
            printf("查无此人\n");
        }
    }
    else if(n==2)
    {
        NODE *t=head;//遍历匹配姓名
        char str[50];
        printf("请输入姓名\n");
        scanf("%s",str);
        int f=0;
        while (t->next!=NULL)
        {
            
            t=t->next;
            if(strcmp(t->pp->name,str)==0)
            {
                printf("编号%d\t姓名:%s\t职位:%s\t部门:%s入职时间:%d\n",t->pp->number,t->pp->name,t->pp->position,t->pp->department,t->pp->YearOfEntry);
                f=1;
            }
            if (t->next==NULL&&f==1)
            {
                break;
            }     
        }
        if(t->next==NULL&&f==0)
        {
            printf("查无此人\n");
        }
    }
    
}
void modification()//修改员工信息
{
    NODE * head=initialize();//更新头文件
    NODE *p=head;//遍历编号
    NODE *t=head;//遍历链表更新文件
    int f=0;
    int a;
    printf("请输入编号\n");
    scanf("%d",&a);
    while (p->next!=NULL)
    {
        p=p->next;
        if(p->pp->number==a)
        {
            printf("请依次输入员工编号 员工姓名 职位 部门 入职年份\n");
            scanf("%d %s %s %s %d",&p->pp->number,p->pp->name,p->pp->position,p->pp->department,&p->pp->YearOfEntry);
            printf("修改成功，编号%d的姓名:%s\t职位:%s\t部门:%s入职时间:%d\n",p->pp->number,p->pp->name,p->pp->position,p->pp->department,p->pp->YearOfEntry);

            f=1;
        }
        if (p->next==NULL&&f==1)
        {
            break;
        } 
    }
    if(p->next==NULL&&f==0)
    {
        printf("查无此人\n");
    } 
    FILE *fp=fopen("staff.txt","w+");//w+是以清空文件的形式打开文件读写
    while (t->next!=NULL)
    {
        t=t->next;
        fprintf(fp,"%d\t%s\t%s\t%s\t%d\n",t->pp->number,t->pp->name,t->pp->position,t->pp->department,t->pp->YearOfEntry);
    }
    fclose(fp);
    FILE * pf=fopen("wage.txt","w+");
    NODE * q=head;//遍历链表更新工资文件
    while (q->next!=NULL)
    {
        q=q->next;
        if(q->mony!=NULL)
        fprintf(pf,"%s %c %d\n",q->pp->name,q->mony->level,q->mony->wage);
    }
    fclose(pf);
}
void display()//显示所有员工信息
{
    FILE * fp=fopen("staff.txt","r");
    char str[100];
    while (fgets(str,100,fp)!=NULL)
    {
        fputs(str,stdout);  
    }
    fclose(fp);
}
#include<time.h>
void wagelevel()//设置员工工资，等级
{
    NODE * head=initialize();//更新头文件
    NODE * p=head;//遍历寻找空的工资链表
    FILE * fp=fopen("wage.txt","a+");
    time_t t;//保存获取的系统时间值  
    srand((unsigned)time(NULL)); // 初始化随机数生成器
    $ * r=malloc(sizeof($));
    while (p->next!=NULL)
    {
        // printf("11111%d\n",p->next->pp->number);
        p=p->next;
        if (p->mony==NULL)
        {
            printf("请输入员工%s的等级\n",p->pp->name);
            getchar();
            scanf("%c",&r->level);
            if (r->level=='I')
            {
                r->wage=4000 + rand() % (5000 - 4000 + 1); 
                p->mony=r;
            }
            else if (r->level=='H')
            {
                r->wage=6000 + rand() % (5000 - 4000 + 1); 
                p->mony=r;
            }
            else if (r->level=='G')
            {
                r->wage=8000 + rand() % (6000 - 4000 + 1); 
                p->mony=r;
            }
            else
            {
                printf("输入错误,请重新输入\n");
                break;
            }
            fprintf(fp,"%s\t%c\t%d\n",p->pp->name,p->mony->level,p->mony->wage);
        }      
    }
    if (p->next==NULL)
    {
        printf("所有员工工资都已分配\n");
    }
    
    fclose(fp);

}


//员工操作界面
void CheckSalary(char * name)//查看本月工资
{
    char str[20];
    char ch;
    int wage;
    FILE * fp=fopen("wage.txt","r");
    while (fscanf(fp,"%s %c %d",str,&ch,&wage)!=EOF)
    {
        if (strcmp(name,str)==0)
        {
            printf("员工%s 等级%c 你的本月工资为%d\n",str,ch,wage);
            return;
        }   
    }
    printf("您的本月工资还未结算");
    fclose(fp);
}
void ChangePassword(char * name,NODE * head)//修改密码
{
    FILE * fp=fopen("passwd.txt","r");
    NODE * p=head;//遍历链表姓名
    int id;
    while (p->next!=NULL)
    {
        p=p->next;
        if (strcmp(p->pp->name,name)==0)
        {
            id=p->pp->number;
            
            break;
        }    
    }

    printf("%s的编号%d\n",name,id);
   
    int a,pwd,i=0,j=0;
    int arr1[100]={0},arr2[100]={0};
    while (fscanf(fp,"%d %d",&arr1[i],&arr2[i])!=EOF)//用数组临时装下更新后的数据
    {
        if (arr1[i]==id)
        {
            printf("员工编号%d请输入新密码:",arr1[i]);
            scanf("%d",&arr2[i]);
        }
        i++;
    }
    fclose(fp);

    FILE * pf=fopen("passwd.txt","w+");
    for (j=0; j <i; j++)//将数组内的内容重新输入文件
    {
        fprintf(fp,"%d %d\n",arr1[j],arr2[j]);
    }
    fclose(pf);
    
    printf("修改密码成功\n");
}
void displaycrycle()//显示员工圈的内容
{
    FILE * fp=fopen("EmployeeCircle.txt","r+");
    char str[100];
    while (fgets(str,100,fp)!=NULL)
    {
        printf("%s",str);
    }
    fclose(fp);
    
}
void EmployeeCircle(char * name)//员工圈
{
    while (1)
    {
        displaycrycle();
        FILE * fp=fopen("EmployeeCircle.txt","a+");
        char str[100];
        printf("请输入你要发送的内容(输入exit退出员工圈):");
        scanf("%s",str);
        if (strcmp(str,"exit")==0)
        {
            return;
        }
        
        fprintf(fp,"%s:%s\n",name,str);
        fclose(fp);
    } 
}
void AdvanceOfWages(NODE * head,char * name)//预支工资
{
    FILE * fp=fopen("tip.txt","w+");
    fputs("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",fp);
    fputs("￥￥￥￥￥￥￥￥￥￥没钱吃饭了？试试预支吧！￥￥￥￥￥￥￥￥￥￥\n",fp);
    fputs("I级:可预支200-400元 H级:可预支400-600元 G级:可预支800-1000元\n",fp);
    fputs("￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥\n",fp);
    fputs("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",fp);
    fputs("------------------请输入你想预支的金额----------------------\n",fp);
    char str[100];
    rewind(fp);
    while (fgets(str,100,fp))
    {
        printf("%s",str);
    }
    fclose(fp);
    char dj;
    int m;
    scanf("%d",&m);
    NODE * p=head;//遍历名字
    while (p->next!=NULL)
    {
        p=p->next;
        if (strcmp(name,p->pp->name)==0)
        {
            dj=p->mony->level;
            break;
        }  
    }
    if (dj=='I')
    {
        if (m<=400&&m>=200)
        {
            p->mony->wage -= m;
            printf("你已成功预支,本月应发工资已减%d\n",m);
        }
        else
        printf("你的预支金额不在要求范围内\n");
    }
    if (dj=='H')
    {
        if (m<=600&&m>=400)
        {
            p->mony->wage -= m;
            printf("你已成功预支,本月应发工资已减%d\n",m);
        }
        else
        printf("你的预支金额不在要求范围内\n");
    }
    if (dj=='G')
    {
        if (m<=1000&&m>=600)
        {
            p->mony->wage -= m;
            printf("你已成功预支,本月应发工资已减%d\n",m);
        }
        else
        printf("你的预支金额不在要求范围内\n");
    }
    NODE * t=head;//遍历链表更新文件
    FILE * pf=fopen("wage.txt","w+");
    while (t->next!=NULL)
    {
        t=t->next;
        if(t->mony!=NULL)
        fprintf(pf,"%s %c %d\n",t->pp->name,t->mony->level,t->mony->wage);
    }
    fclose(pf);
}
int main(int argc,char * argv[])
{
    while (1)
    {
        int n;
        FILE * fp=fopen("staff.txt","a+");
        NODE *head=initialize();
        n=LoginInterface(head);//管理员账号返回“0”，员工账号返回员工编号
        if (n==0)
        {
            while (1)
            {
                InitialInterface(n);
                int ch;
                scanf("%d",&ch);
                switch (ch)
                {
                case 1:
                    addmember();
                    break;
                case 2:
                    Delstaff();
                    break;
                case 3:
                    findmember();
                    break;
                case 4:
                    modification();
                    break;
                case 5:
                    display();
                    break;
                case 6:
                    wagelevel();
                    break;
                case 7:
                    exit(0);
                    break;
                
                default:
                    break;
                }
            }
        }
        else
        {
            NODE * p=head;//遍历链表，根据编码查找姓名
            while (p->next!=NULL)
            {
                p=p->next;
                if (p->pp->number==n)
                {
                    printf("欢迎您%s\n",p->pp->name);
                    break;
                } 
            } 
            while (1)
            {
                InitialInterface(2);//员工界面传2给函数
                int ch;
                scanf("%d",&ch);
                switch (ch)
                {
                case 1:
                    CheckSalary(p->pp->name);
                    break;
                case 2:
                    ChangePassword(p->pp->name,head);
                    break;
                case 3:
                    EmployeeCircle(p->pp->name);
                    break;
                case 4:
                    AdvanceOfWages(head,p->pp->name);
                    break;
                case 5:
                    break;                
                default:
                    break;
                }
                if (ch==5)
                {
                    break;
                }
            }                        
        }
    }
    return 0;
}